package lanou.giftsay_01.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import lanou.giftsay_01.bean.HotBean;
import lanou.giftsay_01.R;
import lanou.giftsay_01.fragment.mine.OnItemClickListener;

/**
 * Created by dllo on 16/8/17.
 */
public class hotRvAdapter extends RecyclerView.Adapter<hotRvAdapter.MyViewHolder> {
   private Context context ;
    private ArrayList<HotBean> arrayList ;
    OnItemClickListener onItemClickListener ;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setArrayList(ArrayList<HotBean> arrayList) {
        this.arrayList = arrayList;
        notifyDataSetChanged();
    }

    public hotRvAdapter(Context context) {
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         View view = LayoutInflater.from(context).inflate(R.layout.item_hot , parent ,false);
         MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        try {
            holder.tv.setText(arrayList.get(0).getData().getItems().get(position).getData().getName());
            holder.like.setText(String.valueOf(arrayList.get(0).getData().getItems().get(position).getData().getFavorites_count()));
            Picasso.with(context).load(arrayList.get(0).getData().getItems().get(position).getData().getCover_image_url()).into(holder.iv);
            if (arrayList.get(0).getData().getItems().get(position).getType().equals("shop_item")){
            holder.title.setText(arrayList.get(0).getData().getItems().get(position).getData().getSkus().get(position).getPrice());
        }
           else{
           holder.title.setText(arrayList.get(0).getData().getItems().get(position).getData().getPrice());
            }

        }catch (NullPointerException e) {

        }
        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.OnClick(position ,arrayList);
            }
        });
   }


    @Override
    public int getItemCount() {
        return arrayList != null ?arrayList.get(0).getData().getItems().size() : 0;

    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv ;
        TextView title ;
        TextView like ;
        ImageView iv ;
        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.hot_tv);
            title = (TextView) itemView.findViewById(R.id.hot_title);
            like = (TextView) itemView.findViewById(R.id.hot_like_count);
            iv = (ImageView) itemView.findViewById(R.id.hot_iv);
        }
    }
}
